function editorMessage(editor, message, showTime, type) {
    editor.fireEvent('showmessage', {
        'id': 'loading_' + (+new Date()).toString(36),
        'content': message,
        'type': type == null ? 'info' : type,
        'timeout': showTime == null ? 4000 : showTime
    });
}
/***************************/
/*
 相关文章功能暂时注释
 修改：黄文伟
 修改时间：2017-8-17
*/
//UE.registerUI('相关文章', function (editor, uiName) {
//    //注册按钮执行时的command命令，使用命令默认就会带有回退操作
//    editor.registerCommand(uiName, {
//        execCommand: function () {
//            //相关文章弹出框
//            dialogWin = openWindow("#form-window", "相关文章", "http://manage.luziqiang.com/VIVI_HY_ADMIN/Module/solr/solr.html", "icon-add").dialog('open');
//        }
//    });
//    //创建一个button
//    var btn = new UE.ui.Button({
//        //按钮的名字
//        name: uiName,
//        //提示
//        title: uiName,
//        //添加额外样式，指定icon图标，这里默认使用一个重复的icon
//        cssRules: 'background-position: -500px 0;',
//        //点击时执行的命令
//        onclick: function () {
//            //这里可以不用执行命令,做你自己的操作也可
//            editor.execCommand(uiName);
//        }
//    });
//    //当点到编辑内容上时，按钮要做的状态反射
//    editor.addListener('selectionchange', function () {
//        var state = editor.queryCommandState(uiName);
//        if (state == -1) {
//            btn.setDisabled(true);
//            btn.setChecked(false);
//        } else {
//            btn.setDisabled(false);
//            btn.setChecked(state);
//        }
//    });
//    //因为你是添加button,所以需要返回这个button
//    return btn;
//});

//注册抓取远程图片
UE.registerUI('catchremoteimage', function (editor, uiName) {
    var totalCount = 0;
    //创建一个button
    var btn = new UE.ui.Button({
        //按钮的名字
        name: "catchremoteimage",
        //提示
        title: "一键替换远程图片",
        //添加额外样式，指定icon图标，这里默认使用一个重复的icon
        cssRules: 'background-position: -581px -40px;',
        //点击时执行的命令
        onclick: function () {
            totalCount = 0;
            //这里可以不用执行命令,做你自己的操作也可
            var catcherLocalDomain = editor.getOpt('catcherLocalDomain'),
                catcherActionUrl = editor.getActionUrl(editor.getOpt('catcherActionName')),
                catcherUrlPrefix = editor.getOpt('catcherUrlPrefix'),
                catcherFieldName = editor.getOpt('catcherFieldName');
            var test = function (src, urls) {
                if (src.indexOf(location.host) != -1 || /(^\.)|(^\/)/.test(src)) {
                    return true;
                }
                if (urls) {
                    for (var j = 0, url; url = urls[j++];) {
                        if (src.indexOf(url) !== -1) {
                            return true;
                        }
                    }
                }
                return false;
            };
            var remoteImages = [];
            var imgs = editor.document.getElementsByTagName("img");
            for (var i = 0, ci; ci = imgs[i++];) {
                if (ci.getAttribute("word_img")) {
                    continue;
                }
                var src = ci.getAttribute("_src") || ci.src || "";
                if (/^(https?|ftp):/i.test(src) && !test(src, catcherLocalDomain)) {
                    //todo 远程操作下载                    
                    remoteImages.push(src);
                }
            }
            if (remoteImages.length) {
                totalCount = remoteImages.length;
                var remoteImage = [];
                editorMessage(editor, '开始下载,共有' + totalCount + " 条需要处理!(网络不好的请下载完后再次点击,直到'提示已无可下载')");
                for (var i = 0; i < remoteImages.length; i++) {
                    //sleep(1000);
                    remoteImage = [];
                    remoteImage.push(remoteImages[i]);
                    catchremoteimage(remoteImage, {
                        //成功抓取
                        success: function (r) {
                            try {
                                var info = r.state !== undefined ? r : eval("(" + r.responseText + ")");
                            } catch (e) {
                                editorMessage(editor, e, 4000, 'error');
                                return;
                            }
                            //console.log(i + ':' + remoteimage);
                            /* 获取源路径和新路径 */
                            var i, j, ci, cj, oldSrc, newSrc, list = info.list;

                            for (i = 0; ci = imgs[i++];) {
                                oldSrc = ci.getAttribute("_src") || ci.src || "";
                                for (j = 0; cj = list[j++];) {
                                    if (oldSrc == cj.source && cj.state == "SUCCESS") {  //抓取失败时不做替换处理
                                        newSrc = catcherUrlPrefix + cj.url;
                                        UE.dom.domUtils.setAttributes(ci, {
                                            "src": newSrc,
                                            "_src": newSrc
                                        });
                                        break;
                                    }
                                }
                            }
                            editor.fireEvent('catchremotesuccess')
                        },
                        //回调失败，本次请求超时
                        error: function (e) {
                            editor.fireEvent("catchremoteerror");
                        }
                    });
                }
            }
            else { editorMessage(editor, '已无可下载的远程图片！'); }

            function catchremoteimage(img, callbacks) {
                var params = UE.utils.serializeParam(editor.queryCommandValue('serverparam')) || '',
                    url = UE.utils.formatUrl(catcherActionUrl + (catcherActionUrl.indexOf('?') == -1 ? '?' : '&') + params),
                    isJsonp = UE.utils.isCrossDomainUrl(url),
                    opt = {
                        'method': 'POST',
                        'dataType': isJsonp ? 'jsonp' : '',
                        'timeout': 60000, //单位：毫秒，回调请求超时设置。目标用户如果网速不是很快的话此处建议设置一个较大的数值
                        'onsuccess': callbacks["success"],
                        'onerror': callbacks["error"]
                    };
                opt[catcherFieldName] = img;
                UE.ajax.request(url, opt);
                //console.log(url);
            }

            function sleep(delay) {
                var startTime = new Date();
                while (new Date() - startTime < delay);
                //var start = new Date().getTime();
                //while (new Date().getTime() < start + delay);
            }
        }
    });

    editor.addListener("catchremotesuccess", function (type) {
        totalCount--;
        if (totalCount == 0) {
            editorMessage(editor, '已全部下载成功！');
        } else {
            editorMessage(editor, '下载成功一条！还有' + totalCount + "条待下载", 800, 'success');
        }
    });

    editor.addListener("catchremoteerror", function (type, arg1, arg2) {
        //alert('一键替换远程图片失败！');
        editorMessage(editor, '下载失败一条！', 2000, 'error');
    });

    //当点到编辑内容上时，按钮要做的状态反射
    editor.addListener('selectionchange', function () {
        var state = editor.queryCommandState(uiName);
        if (state == -1) {
            btn.setDisabled(true);
            btn.setChecked(false);
        } else {
            btn.setDisabled(false);
            btn.setChecked(state);
        }
    });
    //因为你是添加button,所以需要返回这个button
    return btn;
});